<?php

namespace app\api\controller\v1\user;

use app\api\validate\v1\user\UserAgentValidate;
use app\model\advert\Advert;
use app\Request;
use app\services\agent\AgentManageServices;
use Carbon\Carbon;


class UserAgentController extends \app\api\controller\BaseController
{
    protected $services = NUll;

    public function __construct(AgentManageServices $services)
    {
        $this->services = $services;
    }

    /**
     * 分销首页
     */
    public function index(Request $request)
    {
        $info = $this->services->getHomeIndex($request->user());
        return app('json')->success($info);
    }

    public function center(Request $request)
    {
        $info = $this->services->center($request->user());
        return app('json')->success($info);
    }

    /**
     * 分销等级
     */
    public function agentLevel(Request $request)
    {
        $info = $this->services->getLevelList($request->user());
        return app('json')->success($info);
    }


    public function upLevel(Request $request)
    {
        $up_to_level = $request->post('up_to_level', 0);
        $type = $request->post('type', '');
        $this->services->upLevel($request->user(), $up_to_level, $type);
        return app('json')->success(410125);
    }

    /**
     * 分销商品
     */
    public function products(Request $request)
    {
        $limit = $request->get('limit');
        $sort_type = $request->get('sort_type') ?? 1;
        $sort_value = $request->get('sort_value') ?? 1;
        return app('json')->success($this->services->getProducts($request->user(), $sort_type, $sort_value, $limit));
    }

    /**
     * 分销商品详情
     */
    public function productDetail(Request $request)
    {
        $productId = $request->get('product_id');
        if(!$productId) {
            return app('json')->fail(400562);
        }
        $params = ['product_id' => $productId];
        $result = $this->services->productDetail($params);

        return app('json')->success($result);
    }

    /**
     * 分销规则
     */
    public function agentRule(Request $request)
    {
        $result = $this->services->agentRule();

        return app('json')->success($result);
    }

    /**
     * 佣金
     */
    public function commission(Request $request)
    {
        $params = $request->getMore([
            ['sort_type', ''],
        ]);
        $user = $request->user()->toArray();
        $params['user_id'] = $user['uid'];
        $this->baseValidate((new UserAgentValidate()), $request->action(), $params);

        $result = $this->services->commission($params);

        return app('json')->success($result);
    }

    /**
     * 数据分析
     */
    public function analysis(Request $request)
    {
        $params = $request->getMore([
            ['date_type', ''],
            ['time', '']
        ]);
        $user = $request->user()->toArray();
        $params['user_id'] = $user['uid'];
        $this->baseValidate((new UserAgentValidate()), $request->action(), $params);

        $result = $this->services->analysis($params);

        return app('json')->success($result);
    }

    /**
     * 银行卡添加
     */
    public function bankCardAdd(Request $request)
    {
        $params = $request->postMore([
            ['account_name', ''],
            ['card_number', ''],
            ['bank_name', ''],
        ]);
        $user = $request->user()->toArray();
        $params['user_id'] = $user['uid'];
        $this->baseValidate((new UserAgentValidate()), $request->action(), $params);

        $this->services->bankCardAdd($params);

        return app('json')->success(100000);
    }

    public function bankCardEdit(Request $request)
    {
        $params = $request->postMore([
            ['id', 0],
            ['account_name', ''],
            ['card_number', ''],
            ['bank_name', ''],
        ]);
        $user = $request->user()->toArray();
        $params['user_id'] = $user['uid'];

        $this->baseValidate((new UserAgentValidate()), $request->action(), $params);
        $this->services->bankCardEdit($params);

        return app('json')->success(100000);
    }

    public function bankCardDetail(Request $request)
    {

        $info = $this->services->bankCardDetail($request->get('id', 0), $request->uid());
        return app('json')->success($info);
    }

    /**
     * 银行卡列表
     */
    public function bankCardList(Request $request)
    {
        $params = [];
        $user = $request->user()->toArray();
        $params['user_id'] = $user['uid'];

        $result = $this->services->bankCardList($params);

        return app('json')->success($result);
    }

    /**
     * 银行卡删除
     */
    public function bankCardDelete(Request $request)
    {
        $params = $request->postMore([
            ['agent_bank_card_id', ''],
        ]);
        $this->baseValidate((new UserAgentValidate()), $request->action(), $params);

        $this->services->bankCardDelete($params);

        return app('json')->success(100002);
    }

    /**
     * 团队信息
     */
    public function teamInfo(Request $request)
    {
        $params = [];
        $user = $request->user()->toArray();
        $params['user_id'] = $user['uid'];

        $result = $this->services->teamInfo($params);

        return app('json')->success($result);
    }

    /**
     * 团队列表
     */
    public function teamList(Request $request)
    {
        $params = $request->postMore([
            ['account', ''],
            ['start_time', ''],
            ['end_time', ''],
            ['sort_type', ''],
            ['sort_value', '']
        ]);
        $user = $request->user()->toArray();
        $params['user_id'] = $user['uid'];

        $result = $this->services->teamList($params);

        return app('json')->success($result);
    }
}